package storm.irccyn.e4.rcp.wizard.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.eclipse.core.internal.runtime.InternalPlatform;
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.IDocumentPartitioningListener;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.service.log.LogService;


public class ConsoleViewManager implements IDocumentListener{
	public static String prompt="#>";
	private MessageConsole Console;
	
	public static final String ID = "viewcommunication.views.SenderView";

	//gestion evt
	
	
	//@Inject
	//IEventBroker broker;

	public static MessageConsole getConsole(String name) {
		 
	    IConsoleManager conMan = ConsolePlugin.getDefault().getConsoleManager();
	 
	    IConsole[] existing = conMan.getConsoles();
	 
	    for (int i = 0; i < existing.length; i++) {
	        if (name.equals(existing[i].getName())) {
	            return (MessageConsole)existing[i];
	        }
	    }
	 
	    // No console found, so create a new one
	    MessageConsole myConsole = new MessageConsole(name, null);
	   IConsole[] crt =  new IConsole[] { myConsole };
	   
	    conMan.addConsoles(crt);
	    
	  //  myConsole.getDocument().addDocumentPartitioningListener(new ConsoleViewManager());;
	    ConsoleViewManager liste=  new ConsoleViewManager();
	    myConsole.getDocument().addDocumentListener(liste);
	    liste.Console = myConsole;
	  
	    
	  //  myConsole.addPropertyChangeListener(new ConsoleViewManager());
	    System.out.println(" TEST A");
	   
	    
	    return myConsole;
	}
	
	@Override
	public void documentAboutToBeChanged(DocumentEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Inject
	static IEventBroker eventBroker; 
	@Override
	public void documentChanged(DocumentEvent event) {
		BundleContext ctx = InternalPlatform.getDefault().getBundleContext();
		//System.out.println("bundle " + bundle);
		
		 //BundleContext ctx = bundle.getBundleContext();
		//	System.out.println("ctx " + InternalPlatform.getDefault().getBundleContext());

   
		 ServiceReference<EventAdmin> ref = ctx.getServiceReference(EventAdmin.class);
	        EventAdmin eventAdmin = ctx.getService(ref);
	        Map<String,Object> properties = new HashMap<String, Object>();
	        properties.put("DATA", new Date());
	        Event event2 = new Event("viewcommunication/Test", properties);
	        eventAdmin.sendEvent(event2);
	        System.out.println("ctx " + event2);  
	        
	        
	        
	        // LOGGER TEST
	      
	        
		// TODO Auto-generated method stub
		//System.out.println(" TEST  B "+event);
	/*	Object service = PlatformUI.getWorkbench().getService(IEventBroker.class);  
		if (service instanceof IEventBroker) { 
			((IEventBroker) service).post("TEST", new StringBuffer("MESS"));
			System.out.println(event.fText + " " + service);
		}*/
		//System.out.println(event.fText + " " + eventBroker);
		//eventBroker.send("TEST", new StringBuffer("MESS"));
		
		
		//Syncrhone
	    
	    //Asynshrone 
	    //broker.post(MyEventConstants.TOPIC_TODO_NEW, "New data"); 

	//	if (event.fText.contains ("\n")) Console.getDocument().set( Console.getDocument().get()+ ConsoleViewManager.prompt + " " );
		/*try {
			Console.getDocument().addPosition(new Position(1));
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}

	

/*	@Override
	public void documentPartitioningChanged(IDocument document) {
		// TODO Auto-generated method stub
		System.out.println(" TEST  B "+document.getLength());
		try {
			if (document.getChar(document.getLength()-1) == '\n'){
			System.out.println(" TEST  A "+document.getLength());
			document.set( document.get()+ ConsoleViewManager.prompt + " " );
			
			;
			}
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/

	

	
}
